package com.block.datapush.spread.service.type;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson.JSONArray;
import com.block.common.vo.ResponseVo;
import com.block.datapush.common.model.result.FilterHandlerResult;
import com.block.datapush.common.service.ISpreadDataFilterHandlerService;
import com.block.datapush.spread.model.result.SpreadResult;
import com.block.datapush.spread.service.ISpreadService;
import com.block.db.entity.MemberExtInfo;
import com.block.db.entity.SpreadBaseInfo;
import com.block.db.entity.SpreadTaskInfo;
import com.block.db.mapper.SpreadTaskInfoMapper;
import com.block.sms.enums.UseTypeEnum;
import com.block.sms.service.ISmsRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class GzhSmsSpreadService implements ISpreadService {

    @Autowired
    ISpreadDataFilterHandlerService spreadDataFilterHandlerService;
    @Resource
    SpreadTaskInfoMapper spreadTaskInfoMapper;
    @Autowired
    ISmsRecordService smsRecordService;

    @Override
    public SpreadResult spreadExecHandler(MemberExtInfo memberExtInfo, SpreadBaseInfo spreadBaseInfo) {

        List<SpreadTaskInfo> ableEntityList = spreadTaskInfoMapper.getEnableEntityList(spreadBaseInfo.getId());
        if (CollUtil.isNotEmpty(ableEntityList)){

            for (SpreadTaskInfo spreadTaskInfo : ableEntityList) {
                FilterHandlerResult handlerResult = spreadDataFilterHandlerService.execSpreadFilter(spreadTaskInfo, memberExtInfo);

                if (handlerResult.isSuccess()){

                    // 过滤通过，发送短信
                    UseTypeEnum useTypeEnum = UseTypeEnum.SPREAD;
                    String mobile = memberExtInfo.getMobile();
                    String templateId = spreadTaskInfo.getExtData();
                    String ip = memberExtInfo.getReqIp();

                    JSONArray jsonArray = new JSONArray();

                    ResponseVo responseVo = smsRecordService.sendSmsTemplate(useTypeEnum, mobile, templateId, jsonArray, ip);
                }
            }
        }

        SpreadResult  spreadResult = new SpreadResult();
        return spreadResult;
    }
}
